<?php
/**
 * 批量插入几千条官方的示例数据
 * author: JiangJun
 * CreateTime:2021/7/25 21:41
 */


require '../vendor/autoload.php';

use Aws\DynamoDb\Exception\DynamoDbException;
use Aws\DynamoDb\Marshaler;

$sdk = new Aws\Sdk([
    'region'   => 'us-west-2',
    'version'  => 'latest'
]);

$dynamodb = $sdk->createDynamoDb();
$marshaler = new Marshaler();//用于将json或php数组，转成DynamoDb需要的类型

$tableName = 'Movies';

$movies = json_decode(file_get_contents('moviedata.json'), true);

$i=0;
foreach ($movies as $movie) {
    $i++;

    $params = [
        'TableName' => $tableName,
        //通过这个方法，将$movie数组，转换成DynamoDb需要的类型
        'Item' => $marshaler->marshalItem($movie)
    ];

    try {
        $result = $dynamodb->putItem($params);
        echo "Added movie: " . $movie['year'] . " " . $movie['title'] . "\n";
    } catch (DynamoDbException $e) {
        echo "Unable to add movie:\n";
        echo $e->getMessage() . "\n";
        break;
    }

}

echo 'All Done';